package com.datviet.controller.user;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.datviet.common.report.ReportRequest;
import com.datviet.common.report.ReportType;
import com.datviet.dao.impl.UserDAO;
import com.datviet.entity.User;
import com.datviet.service.common.ReportDownloader;
import com.datviet.service.common.security.AuthenticatedService;

@Controller
@RequestMapping(value = { "/usr" })
public class DefaultUserController {
	
	protected static Logger logger = Logger.getLogger("controller");
	
	@Autowired
	private ReportDownloader reportDownloader;
	
	@Autowired
	private UserDAO userDAO;
	
	@Autowired
	private AuthenticatedService authenticatedService;
	
	@RequestMapping(value = { "/", "" })
	public String home() {
		return "user.default";
	}
	
	@RequestMapping(value="/download")
	public void download(HttpServletResponse response) {
		logger.debug("download is call");
		Authentication authentication = authenticatedService.getAuthentication();
		logger.debug("authenticated username: " + authentication.getName());
		List<User> users = userDAO.findAll();
		User user = userDAO.selectUserById(1L);
		ReportRequest<User> reportRequest = new ReportRequest<User>();
		reportRequest.setDataSource(users);
		reportRequest.setGeneratedName("userReport");
		reportRequest.setReportTemplate("/report/user.jrxml");
		reportRequest.setReportType(ReportType.PDF);
		HashMap<String, Object> reportParms = new HashMap<String, Object>();
		reportParms.put("Title", "This title get from parameters");
		reportRequest.setReportParms(reportParms);
		reportDownloader.download(reportRequest, response);
	}	
}
